package com.wmd.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Simple to Introduction
 *
 * @projectName:scs-platform
 * @packageName:com.wmd.iot.badge.util
 * @className:DateTimeUtils
 * @Description:[时间工具]
 * @Author:luyincheng
 * @CreateDate:2021/1/11 9:31
 * @blog:[方法具体上行/下行参数文档]
 */
public class DateTimeUtils {

    public static final String TEMPLATE_1="yyyy-MM-dd HH:mm:ss";

    public static Date StringToDate(String dateTime,String template){
        SimpleDateFormat df=new SimpleDateFormat(template);
        try {
            return df.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * string转timestamp
     * @param str
     * @return
     */
    public static java.sql.Timestamp strToTime(String str){

        String strDate =
                str.substring(0,4) + "-" + str.substring(4,6) + "-" + str.substring(6,8) + " " + str.substring(8,10) + ":" + str.substring(10,12) + ":" + str.substring(12,14);
        if (null == str || str.equals("null")){
            return new java.sql.Timestamp(new Date().getTime());
        }else {
            java.sql.Timestamp date = null;
            SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                java.util.Date time = myFormat.parse(strDate);
                date = new java.sql.Timestamp(time.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date;
        }
    }

    /**
     * 效期增加天数，得到一个新的日期
     * @param date
     * @param day
     * @return
     * @throws Exception
     */
    public static Date addDate(Date date,long day) {
        long time = date.getTime(); // 得到指定日期的毫秒数
        day = day*24*60*60*1000; // 要加上的天数转换成毫秒数
        time+=day; // 相加得到新的毫秒数
        return new Date(time); // 将毫秒数转换成日期
    }
}
